public class Solution3 {
    public String convert(String ss, int numRows) {
        if(numRows == 1) {
            return ss;
        }
        char[] s = ss.toCharArray();
        String ret = "";
        int d = 2 * numRows - 2;
        for(int i = 0; i < numRows; i++) {
            if(i == 0 || i == numRows - 1) {
                for(int j = i; j < s.length; j += d) {
                    ret += s[j];
                }
            }else {
                int j = i;
                int k = d - j;
                while(j < s.length) {
                    ret += s[j];
                    if( k < s.length) {
                        ret += s[k];
                        k += d;
                    }
                    j += d;
                }
            }
        }
        return ret;
    }
    public String convert2(String s, int numRows) {
        if(numRows == 1) {
            return s;
        }
        StringBuilder ret = new StringBuilder();
        int d = 2 * numRows - 2;
        int n = s.length();
        for(int i = 0; i < numRows; i++) {
            //处理第一行和最后一行
            if(i == 0 || i == numRows - 1) {
                for(int j = i; j < n; j += d) {
                    ret.append(s.charAt(j));
                }
            }else {
                //处理中间行
                for(int j = i, k = d - j; j < n || k < n; j += d, k += d) {
                    if(j < n) {
                        ret.append(s.charAt(j));
                    }
                    if(k < n) {
                        ret.append(s.charAt(k));
                    }
                }
            }
        }
        return ret.toString();
    }
}
